<?php
class ModelHkAddress extends Model {
	public function addOrderStatus($data) {
		// var_dump($data);exit;

				$this->db->query("INSERT INTO " . DB_PREFIX . "company_address SET  address_name = '" . $this->db->escape($data['name_cn']) ."', address_name_en = '".$this->db->escape($data['name_en']) . "', sort = '".$this->db->escape($data['sort']) . "', rental = '".$this->db->escape($data['rental']) . "', status = '".$this->db->escape($data['status']) . "'");

				// $company_address_id = $this->db->getLastId();

		// var_dump($company_address_id);exit;
		$this->cache->delete('company_address');
	}

	public function editOrderStatus($company_address_id, $data) {
		// var_dump($data);exit;
			$this->db->query("UPDATE " . DB_PREFIX . "company_address SET address_name = '" . $this->db->escape($data['name_cn']) . "', address_name_en = '".$this->db->escape($data['name_en']) . "' , sort = '" . $this->db->escape($data['sort']) . "', rental = '".$this->db->escape($data['rental']) . "', status = '".$this->db->escape($data['status']) . "'WHERE company_address_id = '" . (int)$company_address_id . "'");

		// var_dump(111);exit;
		$this->cache->delete('company_address');
	}

	public function deleteOrderStatus($company_address_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "company_address WHERE company_address_id = '" . (int)$company_address_id . "'");

		$this->cache->delete('company_address');
	}

	public function getOrderStatus($company_address_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "company_address WHERE company_address_id = '" . (int)$company_address_id . "'");

		return $query->row;
	}

	public function getOrderStatuses($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "company_address WHERE 1=1 ";

			$sql .= " ORDER BY sort";

			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}

			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}

				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}

			$query = $this->db->query($sql);

			return $query->rows;
		} else {
			$project_status_data = $this->cache->get('company_address.' . (int)$this->config->get('config_language_id'));

			if (!$project_status_data) {
				$query = $this->db->query("SELECT company_address_id,address_name FROM " . DB_PREFIX . "company_address WHERE 1=1 ORDER BY company_address_id");

				$project_status_data = $query->rows;

				$this->cache->set('company_address.' . (int)$this->config->get('config_language_id'), $project_status_data);
			}

			return $project_status_data;
		}
	}

	public function getOrderStatusDescriptions($company_address_id) {
		$project_status_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "company_address WHERE company_address_id = '" . (int)$company_address_id . "'");

		foreach ($query->rows as $result) {
			$project_status_data = array('name' => $result['address_name']);
		}

		return $project_status_data;
	}

	public function getTotalOrderStatuses() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "company_address WHERE status = '1' ");

		return $query->row['total'];
	}

		public function getpProjectStatus($company_address_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "company_address WHERE company_address_id = '" . (int)$company_address_id . "'");

		return $query->row;
	}
}